﻿@using MvcAgenda.Resources;
@model IEnumerable<MvcAgenda.Domain.Entities.aevent>
@{
    Layout = null;
    UrlHelper url = new UrlHelper(HttpContext.Current.Request.RequestContext);
    String createUrl = url.RouteUrl(new { Controller = "Events", Action = "Create" });
    String editUrl = url.RouteUrl(new { Controller = "Events", Action = "EditDate" });
}
<script>
    function gettoken() {
        var tokenTag = '@Html.AntiForgeryToken()'
        var token = $(tokenTag).val()
        return token
    }
    function errorHappens() {
        $('.bottom-right').notify({message: { text: "@Events.eventEditDateErrorMsg"}}).show();
        revertFunc();
    }
    $(document).ready(function () {

        $('#calendar').fullCalendar({
            header: {
                left: 'prev,next today',
                center: 'title',
                right: 'month,agendaWeek,agendaDay'
            },
            editable: true,
            fixedWeekCount: false,
            eventLimit: true, // allow "more" link when too many events
            events: [
            @foreach (var item in Model)
            {
    <text>{</text>
            <text>id:'</text> @item.id<text>',</text>
            <text>title:'</text> @item.title<text>',</text>
            <text>description:'</text> @item.description<text>',</text>
            <text>start:'</text> @item.startTime<text>',</text>
	        <text>end:'</text> @item.endTime<text>',</text>
            <text>url:'</text> @item.url<text>',</text>
            <text>eventUrl:'</text> @url.RouteUrl(new { Controller = "Events", Action = "Details", item.id }) <text>',</text>
     <text>},</text>  
            }
			],
            eventClick: function(calEvent, jsEvent, view) {
        if (calEvent.eventUrl) {
            window.open(calEvent.eventUrl,'_blank');
            return false;
        }
    },
    dayClick: function(date, allDay, jsEvent, view) {
       var url = '@createUrl' + '/' + date.format().replace(new RegExp(':', 'g'),'!');
            window.open( url ,'_blank');
            return false;
    },
    eventDrop: function(event, delta, revertFunc, jsEvent, ui, view) {
        var msg = "@Events.eventEditDateConfirmMsg "+ event.title + " at " + event.start.format('LLL');
        bootbox.confirm(msg, function(result) {
                    if (!result) {
            revertFunc();
        }
        else
        {
                var dateEnd = "";
        if (event.end != undefined){
        dateEnd =event.end.format('LLL');
        }
        var formData = {__RequestVerificationToken:gettoken(), id: event.id, strStartTime: event.start.format('LLL'), strEndTime: dateEnd };
        $.ajax({
            url: '@editUrl',
            type: "POST",
            data : formData,
            success: function(data) {
            if (data){
               $('.bottom-right').notify({message: { text: event.title + " @Events.eventEditDateSucessMsg " }}).show();
            }
            else{
                errorHappens();            
            }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                errorHappens();
            }
        });
        }
        }); 


    },
        eventResize: function(event, delta, revertFunc) {
        var dateEnd = "";
        if (event.end != undefined){
        dateEnd =event.end.format('LLL');
        }
        var msg = "@Events.eventEditDateConfirmMsg "+ event.title + " ending at " + dateEnd;
        bootbox.confirm(msg, function(result) {
        if (!result) {
            revertFunc();
        }
        else
        {
        var formData = {__RequestVerificationToken:gettoken(), id: event.id, strStartTime: event.start.format('LLL'), strEndTime: dateEnd };
        $.ajax({
            url: '@editUrl',
            type: "POST",
            data : formData,
            success: function(data) {
            if (data){
                $('.bottom-right').notify({message: { text: event.title + " @Events.eventEditDateSucessMsg " }}).show();
                }
            else{
                errorHappens();
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                errorHappens();
            }
        });
        }
        }); 
    }

        });

    });

</script>
<div id='calendar'>
</div>
